package com.peza.bikelog.common.domain.polar;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HrmData implements Writable{

	private HrmParams params;
	
	private HrmNote note;
	
	private HrmIntTimes intTimes;
	
	private HrmLapNames lapNames;
	
	private HrmIntNotes intNotes;
	
	private HrmHrLimitSummary hrLimitSummary;
	
	private HrmHrThresholdSummary hrThresholdSummary;
	
	private HrmHrZones hrZones;
	
	private HrmTrip trip;
	
	private HrmHrData hrData;
	
	
	public HrmData(String data){
		create(data);
	}
	
	

	public HrmNote getNote() {
		return note;
	}

	public void setNote(HrmNote note) {
		this.note = note;
	}

	public HrmParams getParams() {
		return params;
	}

	public void setParams(HrmParams params) {
		this.params = params;
	}

	public HrmIntTimes getIntTimes() {
		return intTimes;
	}

	public void setIntTimes(HrmIntTimes intTimes) {
		this.intTimes = intTimes;
	}

	public HrmIntNotes getIntNotes() {
		return intNotes;
	}

	public void setIntNotes(HrmIntNotes intNotes) {
		this.intNotes = intNotes;
	}

	public HrmHrLimitSummary getHrLimitSummary() {
		return hrLimitSummary;
	}

	public void setHrLimitSummary(HrmHrLimitSummary hrLimitSummary) {
		this.hrLimitSummary = hrLimitSummary;
	}

	public HrmHrThresholdSummary getHrThresholdSummary() {
		return hrThresholdSummary;
	}

	public void setHrThresholdSummary(HrmHrThresholdSummary hrThresholdSummary) {
		this.hrThresholdSummary = hrThresholdSummary;
	}

	public HrmHrZones getHrZones() {
		return hrZones;
	}

	public void setHrZones(HrmHrZones hrZones) {
		this.hrZones = hrZones;
	}

	public HrmTrip getTrip() {
		return trip;
	}

	public void setTrip(HrmTrip trip) {
		this.trip = trip;
	}

	public HrmHrData getHrData() {
		return hrData;
	}

	public void setHrData(HrmHrData hrData) {
		this.hrData = hrData;
	}
	
	public HrmLapNames getLapNames() {
		return lapNames;
	}

	public void setLapNames(HrmLapNames lapNames) {
		this.lapNames = lapNames;
	}
	
	private void create (String data){

		List<String> lines = Arrays.asList(data.split("\n"));
		
		Iterator<String> i = lines.iterator();

		while (i.hasNext()){
			String line = i.next();
			
			if (line.startsWith("[Params]")){
				
				StringBuffer paramLines = new StringBuffer();
				do {
					line = i.next();
					paramLines.append(line+"\n");
				} while (line.length() > 0);
				
				params = new HrmParams(paramLines.toString());

			} else if (line.startsWith("[Note]")){
				
				StringBuffer noteLines = new StringBuffer();
				do {
					line = i.next();
					noteLines.append(line+"\n");
				} while (line.length() > 0);
				
				note = new HrmNote(noteLines.toString());
				
				
			} else if (line.startsWith("[IntTimes]")){
				
				StringBuffer intTimesLines = new StringBuffer();
				do {
					line = i.next();
					intTimesLines.append(line+"\n");
				} while (line.length() > 0);
				
				intTimes = new HrmIntTimes(intTimesLines.toString());
				
			} else if (line.startsWith("[LapNames]")){
				
				StringBuffer lapNameLines = new StringBuffer();
				do {
					line = i.next();
					lapNameLines.append(line+"\n");
				} while (line.length() > 0);
				
				lapNames = new HrmLapNames(lapNameLines.toString());
				
			} else if (line.startsWith("[IntNotes]")){
				
				StringBuffer intNotesLines = new StringBuffer();
				do {
					line = i.next();
					intNotesLines.append(line+"\n");
				} while (line.length() > 0);
				
				intNotes = new HrmIntNotes(intNotesLines.toString());
				
			} else if (line.startsWith("[Summary-123]")){
				
				StringBuffer hrLimitData = new StringBuffer();
				do {
					line = i.next();
					hrLimitData.append(line+"\n");
				} while (line.length() > 0);
				
				hrLimitSummary = new HrmHrLimitSummary(hrLimitData.toString());
				
			} else if (line.startsWith("[Summary-TH]")){
				
				StringBuffer hrThresholdData = new StringBuffer();
				do {
					line = i.next();
					hrThresholdData.append(line+"\n");
				} while (line.length() > 0);
				
				hrThresholdSummary = new HrmHrThresholdSummary(hrThresholdData.toString());
				
			} else if (line.startsWith("[HRZones]")){
				
				StringBuffer hrZonesData = new StringBuffer();
				do {
					line = i.next();
					hrZonesData.append(line+"\n");
				} while (line.length() > 0);
				
				hrZones = new HrmHrZones(hrZonesData.toString());
				
			} else if (line.startsWith("[Trip]")){
				
				StringBuffer tripLines = new StringBuffer();
				do {
					line = i.next();
					tripLines.append(line+"\n");
				} while (line.length() > 0);
				
				trip = new HrmTrip(tripLines.toString());
				
			} else if (line.startsWith("[HRData]")){
				
				StringBuffer hrDataLines = new StringBuffer();
				do {
					line = i.next();
					
					final String REGEX = "^[0-9]+";
					Pattern p = Pattern.compile(REGEX);
					Matcher m = p.matcher(line);
					if (m.find()) {
						hrDataLines.append(line+"\n");
					}
				} while (i.hasNext() && line.length() > 0);
				
				hrData = new HrmHrData(hrDataLines.toString());
				
			}
		}
	}

	public void enableSpeed(){
		params.getsMode().setHrCCdata(true);
		params.getsMode().setSpeed(true);
		params.getsMode().setAltitude(true);
	}
	
	
	@Override
	public String toFileString() {

		StringBuffer buffer = new StringBuffer();
		buffer.append(params.toFileString());
		buffer.append(note.toFileString());
		buffer.append(intTimes.toFileString());
		buffer.append(intNotes.toFileString());
		buffer.append(hrLimitSummary.toFileString());
		buffer.append(hrThresholdSummary.toFileString());
		buffer.append(hrZones.toFileString());
		buffer.append(trip.toFileString());
		buffer.append(hrData.toFileString());
		
	
		return buffer.toString();
	}
	
}
